/*
 * Copyright 2011 Clean GWT
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.cleangwt.showcase.client.action;

import com.cleangwt.bootstrap.client.ui.Modal;
import com.cleangwt.showcase.client.ProvidesSource;
import com.cleangwt.showcase.client.activity.CwUserDetail;
import com.cleangwt.uibinder.client.ActionManager;
import com.cleangwt.uibinder.client.UiHandler;
import com.cleangwt.user.client.ui.ValueCallback;
import com.cleangwt.web.client.Requests;
import com.cleangwt.web.client.StatedCallback;
import com.google.gwt.event.dom.client.ClickEvent;

/**
 * 
 * @author Jess
 * @version 1.0
 * @since 1.0
 */
public abstract class UserDetailAction extends ActionManager<CwUserDetail> implements
    ProvidesSource {

  @UiHandler("btnCancel")
  void onBtnCancelClick(ClickEvent event) {
    view.forward(0);
  }

  @UiHandler("btnDelete")
  void onBtnDeleteClick(ClickEvent event) {
    Modal.confirm(view.c.confirm(), new ValueCallback<String>() {

      @Override
      public void onValueCallback(String option) {
        Requests.postObject("user/delete.do", view.record, new StatedCallback() {

          @Override
          protected void onSuccess(String result) {
            Modal.message(view.c.deleteSucceed(), new ValueCallback<String>() {

              @Override
              public void onValueCallback(String option) {
                view.forward(0);
              }
            });
          }
        });

      }
    });
  }

  @UiHandler("btnReset")
  void onBtnResetClick(ClickEvent event) {
    view.setFormData(view.record);
    view.validator.reset();
  }

  @UiHandler(value = "btnSave", validate = "validator")
  void onBtnSaveClick(ClickEvent event) {
    view.record = view.getFormData(view.record);
    Requests.postObject("user/save.do", view.record, new StatedCallback() {

      @Override
      protected void onSuccess(String result) {
        Modal.message(view.c.saveSucceed(), new ValueCallback<String>() {

          @Override
          public void onValueCallback(String option) {
            view.forward(0);
          }
        });
      }

    });
  }

}
